package com.dianqiu.common.web.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dianqiu.base.action.BaseAction;
import com.dianqiu.common.bean.DqClubsTO;
import com.dianqiu.common.bean.DqLeaguesTO;
import com.dianqiu.common.bean.DqMatchHistTO;
import com.dianqiu.common.bean.DqMatchTO;
import com.dianqiu.common.web.form.CreateMatchFormBean;

import java.sql.Timestamp;

public class CreateMatchAction extends BaseAction {
	/**
	 * 
	 */

	private CreateMatchFormBean createMatchFormBean = new CreateMatchFormBean();

	public String create() throws Exception {

		HttpServletRequest request = this.getRequest();
		List<DqLeaguesTO> leaguesList = this.getCreateMatchService()
				.getAllLeagues();
		request.setAttribute("leaguesList", leaguesList);
		return "create";
	}

	public String getClubsByLeague() throws Exception {

		HttpServletRequest request = this.getRequest();
		HttpServletResponse response = this.getServletResponse();
		response.setContentType("text/xml");
		String leagueIdStr = request.getParameter("LeagueId");
		response.getWriter().print(
				this.getCreateMatchService().bindClubsByLeagueId(leagueIdStr));
		return null;
	}

	public String save() throws Exception {

		HttpServletRequest request = this.getRequest();
		String leagues = request.getParameter("leagues");
		String hometeam = request.getParameter("hometeam");
		String visitingteam = request.getParameter("visitingteam");
		String matchdDataAndTime = request.getParameter("matchdate") + " "
				+ request.getParameter("matchtime");

		if (leagues != null && hometeam != null && visitingteam != ""
				&& leagues != "" && hometeam != "" && visitingteam != ""
				&& matchdDataAndTime != "") {

			Integer hometeamId = null;
			Integer visitingteamId = null;
			Integer leaguesId = null;

			leaguesId = Integer.valueOf(leagues);
			hometeamId = Integer.valueOf(hometeam);
			visitingteamId = Integer.valueOf(visitingteam);

			List<DqLeaguesTO> leagueList = this.getCreateMatchService()
					.getLeaguesById(leaguesId);

			List<DqClubsTO> homeClubList = this.getCreateMatchService()
					.getClubesById(hometeamId);
			List<DqClubsTO> awayClubList = this.getCreateMatchService()
					.getClubesById(visitingteamId);

			if (leagueList != null && homeClubList != null
					&& awayClubList != null) {

				DqLeaguesTO league = leagueList.get(0);
				DqClubsTO homeClub = homeClubList.get(0);
				DqClubsTO awayClub = awayClubList.get(0);

				DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				format.setLenient(false);
				Timestamp matchDate = new Timestamp(format.parse(
						matchdDataAndTime).getTime());

				DqMatchTO match = new DqMatchTO();
				match.setDqClubsByAwayClbId(homeClub);
				match.setDqClubsByHomeClbId(awayClub);
				match.setDqLeagues(league);
				match.setMthDate(matchDate);
				this.createInitialize(match, request);

				this.getCreateMatchService().save(match);

				DqMatchHistTO matchHist = new DqMatchHistTO();
				matchHist.setMthId(match.getMthId());
				matchHist.setDqClubsByAwayClbId(homeClub);
				matchHist.setDqClubsByHomeClbId(awayClub);
				matchHist.setDqLeagues(league);
				matchHist.setMthDate(matchDate);
				this.createInitialize(matchHist, request);

				this.getCreateMatchService().save(matchHist);
				return "success";

			} else {
				return "fail";
			}

		} else {
			return "fail";
		}

	}
}
